
//
// $(function () {
//    $("#btn").click(function () {
//        $(".mask-wrapper").show();
//        $("#body").css({"background":"#ccc"});
//    });
//    $(".close-btn").click(function () {
//        $(".mask-wrapper").hide();
//        $("#body").css({"background":"#fff"});
//    })
// });


function Auth() {
    var self = this;
    self.maskWrapper = $('.mask-wrapper');
    self.closeBtn = $('.close-btn');
    self.scrollWrapper = $(".scroll-wrapper");
};

Auth.prototype.run = function () {
    var self = this;
    self.listenShowHideEvent();
    self.listenSwitchEvent();
    self.listenSigninEvent();
};

Auth.prototype.showEvent = function () {
    var self = this;
    self.maskWrapper.show();
};

Auth.prototype.hideEvent = function () {
    var self = this;
    self.maskWrapper.hide();
};

Auth.prototype.listenShowHideEvent = function () {
    var self = this;
    var signinBtn = $('.signin-btn');
    var signupBtn = $(".signup-btn");
    var closeBtn =$('.close-btn');
    signinBtn.click(function () {
        self.showEvent();
        self.scrollWrapper.css({"left":0});
    });
    signupBtn.click(function () {
        self.showEvent();
        self.scrollWrapper.css({'left':-400});
    });
    closeBtn.click(function () {
        self.hideEvent();
    });
};

Auth.prototype.listenSwitchEvent = function () {
    var self = this;
    var switcher = $(".switch");
    switcher.click(function () {
        // 获取这个元素的“left”
        var currentLeft = self.scrollWrapper.css("left");
        // 将“left”转换为整型，方便运算
        currentLeft = parseInt(currentLeft);
        if(currentLeft<0){
            // animated这个函数会使过度有个时间，而css函数会使过度没时间，体验效果不好
            self.scrollWrapper.animate({"left":"0"});
        }else{
            self.scrollWrapper.animate({"left":"-400px"});
        }
    });
};

Auth.prototype.listenSigninEvent = function () {
    var self = this;
    var signinGroup =$(".signin-group");
    var telephoneInput = signinGroup.find("input[name='telephone']");
    var passwordInput = signinGroup.find("input[name='password']");
    var rememberInput = signinGroup.find("input[name='remember']");
    var submitInput = signinGroup.find(".submit-btn");
    submitInput.click(function () {
        var telephone = telephoneInput.val();
        var password = passwordInput.val();
        // checkbox需要这样获取数据
        var remember = rememberInput.prop('checked');

        xfzajax.post({
            'url':'/account/login/',
            'data':{
                'telephone':telephone,
                'password':password,
                // remember值是1还是0
                'remember':remember?1:0
            },
            // result是返回值有code,message,data
            'success':function (result) {
                if (result['code']==200){
                    self.hideEvent();
                    window.location.reload();
                }else{
                    var messageObject = result['message'];
                    if (typeof messageObject=="string" || messageObject.constructor==String){
                        window.messageBox.show(messageObject);
                    }else{
                        //这种格式 {'password':['密码最大长度不能超过20位'，'密码最短不能少于6位'],'telephone':['电话号码长度不正确'，'电话号码最长11位']}
                        // 遍历messageObject
                        for (var key in messageObject){
                            // 得到messageObject对应的键的值
                            var messages = messageObject[key];
                            // 得到值的第一项值
                            var message = messages[0];
                            window.messageBox.show(message);
                        }
                    }
                }
            },
            'fail':function (error) {
                console.log("2254");
                alert("5425");
                console.log(error);
            },
        });
    });
};

$(function () {
    var auth = new Auth();
    auth.run()
});